"""
@version:python3
@author:ll
@file:03-QObject.py
@time:2022/2/2 1:40
"""
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("QObject的学习")
        self.resize(500, 500)
        self.setup_ui()

    def setup_ui(self):  # 添加子控件以及子控件的配置操作
        self.QObject_test()

    def QObject_test(self):
        # 继承结构测试
        # mros = QObject.mro()
        # for mro in mros:
        #     print(mro)

        # 对象名称和属性的操作
        # obj = QObject()
        # obj.setObjectName("notice")
        # print(obj.objectName())
        #
        # obj.setProperty("notice_level","error")
        # obj.setProperty("notice_level2", "warning")
        # print(obj.property("notice_level"))
        # print(obj.dynamicPropertyNames())

        # 对象的父子关系操作
        # 测试API_START
        # obj0 = QObject()
        # obj1 = QObject()
        # obj2 = QObject()
        # obj2.setObjectName("2")
        # obj3 = QObject()
        # obj3.setObjectName("3")
        # obj4 = QObject()
        # obj5 = QObject()
        # label = QLabel()
        # label.setParent(obj0)

        # print("obj0",obj0)
        # print("obj1",obj1)
        # print("obj2",obj2)
        # print("obj3",obj3)
        # print("obj4",obj4)
        # print("obj5",obj5)
        # obj4.setParent(obj2)
        # obj5.setParent(obj2)
        # obj3.setParent(obj1)
        # obj2.setParent(obj0)
        # obj1.setParent(obj0)
        # 一个对象的父对象只能设置一个，且以后设置的为主
        # print(obj5.parent())
        # print(obj4.parent())
        # print(obj3.parent())
        # print(obj2.parent())
        # print(obj1.parent())
        #
        # print(obj0.children())
        # 通过childen()获得的子对象是直接的子对象
        #


        # 获取一个指定名称和类型的子对象
        # print(obj0.findChild(QObject)) # 这里只能获得一个结果
        # label.setText("2")
        # print(obj0.findChild(QLabel)) # 会报错 参数1必须是QWidget类型而不是QObject类型 对于这种具体控件要求父控件是可见的才能添加
        # print(obj0.findChild(QObject,"2")) # 可以获得指定名称的子对象
        # print(obj0.findChild(QObject,"3")) # 可以获得指定名称的子对象（默认递归查找）
        # print(obj0.findChild(QObject,"3",Qt.FindDirectChildrenOnly)) # 只查找直接子对象，无法获得结果
        # print(obj0.findChildren(QObject))  # 这里可以获得所有结果

        # 测试API_END

        # 内存管理机制_START
        obj1 = QObject()
        obj2 = QObject()
        self.obj1 = obj1
        obj2.setParent(obj1)

        # 监听obj2是否被释放
        obj2.destroyed.connect(lambda :print("obj2被释放了"))
        del self.obj1

        # 内存管理机制_END


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    window = Window()
    window.show()

    sys.exit(app.exec_())
